package com.pandacc.common.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 给spring controller 提供辅助类，例如返回json的ajax响应
 * @author Administrator
 *
 */
public class ControllerUtils {

	private static Log logger = LogFactory.getLog(ControllerUtils.class);
	
	public static void responseWithJson(Object obj, HttpServletResponse res){
		ObjectMapper mapper = new ObjectMapper();
		PrintWriter pw;
		try {
			String out = mapper.writeValueAsString(obj);
			pw = res.getWriter();
			pw.write(out);
		} catch (IOException e) {
			logger.error("Failed to response the ajax: " + e.getMessage());
			e.printStackTrace();
		}
	}
}
